package demo_01;

import java.sql.*;

public class SelectDemo_01 {
    public static void main(String[] args) {

        int r=0;
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/jdbc" +
                "?useUnicode=true&characterEncoding=utf-8";
        String user = "root";
        String password = "admin";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out.println("驱动类不存在!");
            e.printStackTrace();
        }
        Connection conn = null;
        Statement stmt = null;
        ResultSet rst = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            String sql = "SELECT b_id a,b_name b,b_price c\n" +
                    "FROM tbl_book_info";
            stmt = conn.createStatement();
            rst = stmt.executeQuery(sql);
            //判断是否有下一行数据
            //如果有返回true，否则返回false
            while(rst.next()){
                //如果对类型要求不高，可以统一使用String
                int id = rst.getInt("a");
                String name = rst.getString("b");
                float price = rst.getFloat(3);
                System.out.println(id+"  "+name+"  "+price);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
